package com.hualan.sfzy.controller.mindanswer;


import com.hualan.sfzy.entity.mindanswer.Problem;
import com.hualan.sfzy.service.mindanswer.ProblemService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/problem")
public class ProblemController {
    @Autowired
    private ProblemService service;
    @PostMapping
    public Result add(@RequestBody Problem problem){
        return service.add(problem)>0?Result.success():Result.error();
    }
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Integer id){
        return service.remove(id)>0?Result.success():Result.error();
    }
    @DeleteMapping
    public Result removeBatch( Integer[] ids){
        return  service.removeBatch(ids)>0?Result.success():Result.error();
    }
    @PutMapping
    public Result edit(@RequestBody Problem problem){
        return   service.edit(problem)>0?Result.success():Result.error();
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Problem problem= service.findById(id);
        return Result.success(problem);
    }
    @GetMapping("/page")
    public Result findPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize, Problem problem){
        Map<String,Object> map= service.findByPage(pageNum,pageSize,problem);
        return Result.success(map);
    }

    @GetMapping("/page/user")
    public Result findPageUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize, Problem problem){
        Map<String,Object> map= service.findByPageUser(pageNum,pageSize,problem);
        return Result.success(map);
    }

    @GetMapping
    public Result find(){
        List<Problem> list= service.find();
        return Result.success(list);
    }

}
